package beans;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.mysql.jdbc.StringUtils;



public class UserDAO {
	
	public boolean login(String userno,String pwd) {
		Connection con=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		
		boolean flag=false;
		
		try {
			con=DBConnection.getConnection();
			String sql="select * from user";
			pst=con.prepareStatement(sql);
			rs=pst.executeQuery();
			if(rs.next()) {
				System.out.println("ok");
				flag=true;
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			DBConnection.closeConnection(con, pst, rs);
		}
		return flag;		
	}
	
	public Map<String, Object> findone(String userno) {
		System.out.println("进入findone 参数userno is:"+userno);
		Connection con=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		try {
			con=DBConnection.getConnection();
			String sql="select * from user where user_no=?";
			pst=con.prepareStatement(sql);
			pst.setString(1, userno);
			rs=pst.executeQuery();
			String[] keys= {"userno","userpwd","username","usersex","userage","useraddress","userprofession","userinterest","useremail","userphoto","usersummary"};
			
			if(rs.next()) {
				for(int i=0;i<keys.length;i++) {
					map.put(keys[i], rs.getString(i+2));
				}
			}			
		} catch (Exception e) {
			// TODO: handle exception
			DBConnection.closeConnection(con, pst, rs);
		}
		System.out.println("findone is:"+map.toString());
		return map;		
	}

	
	public boolean insert(String userno,String userpwd,String username,String usersex,String userage,String useraddress,String userprofession,String userinterest,String useremail,String userphoto,String usersummary) {
		Connection con=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		
		boolean flag=false;
		try {
			con=DBConnection.getConnection();
			String sql="insert into user values(null,?,?,?,?,?,?,?,?,?,?,?)";
			pst=con.prepareStatement(sql);
			pst.setString(1, userno);
			pst.setString(2, userpwd);
			pst.setString(3, username);
			pst.setString(4, usersex);
			pst.setString(5, userage);
			pst.setString(6, useraddress);
			pst.setString(7, userprofession);
			pst.setString(8, userinterest);
			pst.setString(9, useremail);
			pst.setString(10, userphoto);
			pst.setString(11, usersummary);
			int n=pst.executeUpdate();
			if(n>=1) {
				flag=true;
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			DBConnection.closeConnection(con, pst, rs);
		}
		return flag;
	}
	
	
	public ArrayList<Map<String,Object>> findall(){
		Connection con=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		ArrayList<Map<String, Object>> al=new ArrayList<>();

		try {
			con=DBConnection.getConnection();
			String sql="select * from user";
			pst=con.prepareStatement(sql);
			
			rs=pst.executeQuery();
			String[] keys= {"userno","userpwd","username","usersex","userage","useraddress","userprofession","userinterest","useremail","userphoto","usersummary"};
			while(rs.next()) {
				Map<String, Object> map=new HashMap<>();
				for(int i=0;i<keys.length;i++) {
					map.put(keys[i], rs.getString(i+2));
				}
				al.add(map);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			DBConnection.closeConnection(con, pst, rs);
		}
		return al;
	}
	
	public ArrayList<Map<String,Object>> findwhere(String sql){
		Connection con=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		ArrayList<Map<String, Object>> al=new ArrayList<>();

		try {
			con=DBConnection.getConnection();
			
			pst=con.prepareStatement(sql);
			
			rs=pst.executeQuery();
			String[] keys= {"userno","userpwd","username","usersex","userage","useraddress","userprofession","userinterest","useremail","userphoto","usersummary"};
			while(rs.next()) {
				Map<String, Object> map=new HashMap<>();
				for(int i=0;i<keys.length;i++) {
					map.put(keys[i], rs.getString(i+2));
				}
				al.add(map);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			DBConnection.closeConnection(con, pst, rs);
		}
		return al;
	}
	
	public boolean update(String userno,String userpwd,String username,String usersex,String userage,String useraddress,String userprofession,String userinterest,String useremail,String userphoto,String usersummary) {
		System.out.println("进入更新函数，得到的参数是：");
		System.out.println(userno+userpwd+username+usersex+userage+useraddress+userprofession+userphoto+useremail+usersummary);
		Connection con=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		
		boolean flag=false;
		try {
			con=DBConnection.getConnection();			
			String sql="update user set user_no=?,user_pwd=?,user_name=?,user_sex=?,user_age=?,user_address=?,user_profession=?,user_interest=?,user_email=?,user_photo=?,user_summary=? where user_no=?";
			pst=con.prepareStatement(sql);
			pst.setString(1, userno);
			pst.setString(2, userpwd);
			pst.setString(3, username);
			pst.setString(4, usersex);
			pst.setString(5, userage);
			pst.setString(6, useraddress);
			pst.setString(7, userprofession);
			pst.setString(8, userinterest);
			pst.setString(9, useremail);
			pst.setString(10, userphoto);
			pst.setString(11, usersummary);
			pst.setString(12, userno);
			int n=pst.executeUpdate();
			if(n>=1) {
				flag=true;
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			DBConnection.closeConnection(con, pst, rs);
		}
		return flag;
	}

}
